package com.lee.codeconfig;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import com.lee.codeconfig.entities.Student;
import com.lee.codeconfig.mappers.StudentMapper;

/**
 * Hello world!
 *
 */
public class App {

	public static void main(String[] args) {
		BigDecimal ten = new BigDecimal(10);
		BigDecimal three = new BigDecimal(3);
		BigDecimal result = ten.divide(three, 2, BigDecimal.ROUND_HALF_UP); // 无限循环小数会报java.lang.ArithmeticException:
		System.out.println(result.toString());

		List<String> list = Collections.emptyList(); // emptyList()返回的不支持add，remove操作
//		list.add("a");
	}

	public static void main2(String[] args) {

		// 纯代码方式访问
		DataSource dataSource = new PooledDataSource("com.mysql.cj.jdbc.Driver",
				"jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2b8",
				"root", "1234");
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment environment = new Environment("development", transactionFactory, dataSource);
		Configuration configuration = new Configuration(environment);
		configuration.addMapper(StudentMapper.class);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
		try (SqlSession session = sqlSessionFactory.openSession()) {
			Student student = session.getMapper(StudentMapper.class).selectStudent(1);
			System.out.println(student);
		}
	}
}
